/*
 * @Author: FanWeiHua
 * @Date: 2021-06-27 14:42:22
 * @LastEditTime: 2021-06-27 14:44:44
 * @Description: 组件按需加载
 */
'use strict';
import React from 'react';

export default function asyncComponent(importComponent) {
  class AsyncComponent extends React.Component {
    constructor(props) {
      super(props);
      this.state = { component: null };
    }

    async componentDidMount() {
      const { default: component } = await importComponent();
      this.setState({ component });
    }

    render() {
      const C = this.state.component;
      return C ? <C {...this.props} /> : null;
    }
  }

  return AsyncComponent;
}
